package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.ReturnMoneyModel;
import com.btkjgc.models.extend.ReturnMoneyReceiveModel;
import com.btkjgc.service.intf.ReturnMoneyManageService_Intf;

@Controller
public class ReturnMoneyManageController {

	static Logger logger = Logger.getLogger(ReturnMoneyManageController.class);

    @Autowired
	private ReturnMoneyManageService_Intf returnMoneyManageService;

	@RequestMapping(value = "return_money_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,ReturnMoneyModel returnMoneyModel) {

		ModelAndView mv = new ModelAndView("return_money_manage");
		getReturnMoneyList(request, mv,returnMoneyModel);
		return mv;
	}
	
 
	@RequestMapping(value = "return_money_receive_list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getReturnMoneyReceiveList(HttpServletRequest request,ReturnMoneyReceiveModel returnMoneyReceiveModel) {

		ModelAndView mv = new ModelAndView("return_money_receive_list");
		getReturnMoneyReceiveList(request, mv,returnMoneyReceiveModel);
		return mv;
	}
	
	
	private void getReturnMoneyReceiveList(HttpServletRequest request,
			ModelAndView mv, ReturnMoneyReceiveModel returnMoneyReceiveModel) {
		// TODO Auto-generated method stub
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "aut.tel","like", returnMoneyReceiveModel.getTel()};
		Object[] arr2 = { "aut.account","like", returnMoneyReceiveModel.getAccount()};
		Object[] arr3 = { "aut2.id","=", returnMoneyReceiveModel.getCompanyid()};
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		

		String sql ="SELECT\n" +
				"	rmrt.*, aut.account,\n" +
				"	aut.`name` accountName,\n" +
				"  aut.tel,\n" +
				"  cat.carbrand carplate,\n" +
				"  ct.name cartype,\n" +
				"  aut2.name companyName\n" +
				"FROM\n" +
				"	return_money_receive_t rmrt\n" +
				"LEFT JOIN account_user_t aut ON rmrt.accountid = aut.id AND aut.del = 0\n" +
				"LEFT JOIN car_account_t cat on cat.accountid=aut.id and cat.del=0\n" +
				"LEFT JOIN cartype_t ct on ct.del=0 and cat.cartypeid = ct.id\n" +
				"LEFT JOIN car_t cart on cart.del=0 and cart.id=cat.carid  \n" +
				"LEFT JOIN account_user_t aut2 ON cart.companyid = aut2.id  AND aut2.del = 0\n" +
				"WHERE\n" +
				"	rmrt.del = 0 and rmrt.retuernMoneyid="+returnMoneyReceiveModel.getRetuernMoneyid();

		sql = returnMoneyManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = returnMoneyManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<ReturnMoneyReceiveModel> returnMoneyReceiveModelList = returnMoneyManageService.getList(
				ReturnMoneyReceiveModel.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		returnMoneyManageService.setReturnMoneyReceiveListData(request, mv, pageIndex, totalRecord, returnMoneyReceiveModelList);
	}


	@RequestMapping(value = "view_return_money", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView viewPckageFare(HttpServletRequest request,ReturnMoneyModel returnMoneyModel)
	{
		 ModelAndView mv = new ModelAndView("add_return_money");
		 
		 if ("edit".equals(returnMoneyModel.getTask())) {
			 returnMoneyManageService.setEditReturnMoneydata(request,mv,returnMoneyModel.getId());
		 }
		
		 return mv;
	}

	@RequestMapping(value = "edit_return_money", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public AjaxObj editReturnMoney(ReturnMoneyModel returnMoneyModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		AjaxObj ajaxObj=returnMoneyManageService.updateReturnMoneyModel(returnMoneyModel);

		return ajaxObj;
	}

 
	@SuppressWarnings("unchecked")
	private void getReturnMoneyList(HttpServletRequest request, ModelAndView mv,ReturnMoneyModel returnMoney) {
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "rmt.name","like", returnMoney.getName()};
		paramList.add(arr1);
		

		String sql ="SELECT\n" +
				"	rmt.*, aut.username opername,\n" +
				"	CASE rmt. STATUS\n" +
				"		WHEN 1 THEN '开启'\n" +
				"		WHEN 0 THEN '暂停'\n" +
				"	END statusName\n" +
				"FROM\n" +
				"	return_money_t rmt\n" +
				"LEFT JOIN admin_user_t aut ON aut.id = rmt.operid AND aut.del = 0\n" +
				"WHERE	rmt.del = 0";

		sql = returnMoneyManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = returnMoneyManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<ReturnMoneyModel> returnMoneyModellList = returnMoneyManageService.getList(
				ReturnMoneyModel.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		returnMoneyManageService.setReturnMoneyListData(request, mv, pageIndex, totalRecord,
				returnMoneyModellList);

	}

	
 

	@RequestMapping(value = "add_return_money/{a}")
	@ResponseBody
	public   AjaxObj jsonAddReturnMoneyModel(HttpServletRequest request,
			@PathVariable("a") int a, ReturnMoneyModel returnMoneyModel) {
 
		AjaxObj ajaxObj = null;
		if (returnMoneyModel.getName().length() > 0) {
 
			  ajaxObj = returnMoneyManageService
					.insertReturnMoneyModel(returnMoneyModel);
			

		}
		return ajaxObj;
	}

	
	@RequestMapping(value = "del_return_money", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String  delReturnMoney(HttpServletRequest request, String id) {
 
		returnMoneyManageService.updateDelById(id);
 
		return "redirect:/return_money_manage";
	}
	
	@RequestMapping(value = "update_status_return_money", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String pauseReturnMoney(HttpServletRequest request, String id,String status) {
 
		returnMoneyManageService.updateStatusById(id,status);
	 
		return "redirect:/return_money_manage";
	}
 
	
	
}
